home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-03 | 14.3 KB | 440 lines | [TEXT/MPS ] |
- // UGeometry.h
- // Copyright © 1984-96 by Apple Computer, Inc. All rights reserved.
-
- #ifndef __UGEOMETRY__
- #define __UGEOMETRY__
-
- // MacApp
-
- #ifndef __GEOMETRY__
- #include "Geometry.h"
- #endif
-
- // for CCharString types
- #ifndef __PASCALSTRING__
- #include "PascalString.h"
- #endif
-
- // Toolbox
-
- #ifndef __TYPES__
- #include <Types.h>
- #endif
-
-
- //----------------------------------------------------------------------------------------
- // Constants
- //----------------------------------------------------------------------------------------
-
- const short kMaxCoord = 30000;
-
- //----------------------------------------------------------------------------------------
- // Forward and external typedefs and class declarations.
- //----------------------------------------------------------------------------------------
-
- class VPoint;
- class VRect;
-
- typedef class VPoint *VPointPtr;
-
- typedef class VRect *VRectPtr;
-
-
- //----------------------------------------------------------------------------------------
- // VCoordinates are 32 bits as compared to QD Coordinates which are 16 bits
- //----------------------------------------------------------------------------------------
-
- typedef long VCoordinate;
-
-
- //----------------------------------------------------------------------------------------
- // VPoint: is a toolbox compatible class for the old struct VPoint. It is bitwise
- // compatible with the old struct because it contains no virtual functions.
- //----------------------------------------------------------------------------------------
-
- class VPoint
- {
- public:
- VCoordinate v;
- VCoordinate h;
-
- //------------------------------------------------------------------------------------
- // Constructors
- //------------------------------------------------------------------------------------
-
- inline VPoint()
- { }
-
- inline VPoint(VCoordinate iH, VCoordinate iV) :
- v(iV),
- h(iH)
- { }
-
- inline VPoint(const CPoint& pt) :
- v(pt.v),
- h(pt.h)
- { }
-
- #if qPowerPC
- // copy constructor
- inline VPoint(const VPoint& pt)
- { *(double*)this = *(const double*)&pt; }
- #endif
-
- //------------------------------------------------------------------------------------
- // Copy and conversion operator methods
- //------------------------------------------------------------------------------------
-
- #if qPowerPC
- inline VPoint& operator=(const VPoint& pt)
- { *(double*)this = *(const double*)&pt; return *this; }
- // cheat, we know that we're eight bytes long
- #endif
-
- #if qDebug
- // Conversion operator, for converting VPoint to a textual representation of a
- // VPoint. "VPoint(h,v)".
- inline operator const char*() const
- {
- char textPoint[64];
- sprintf (textPoint, "VPoint(%d, %d)", h, v);
- return CChar63(textPoint);
- }
- #endif
-
- inline CPoint AsPoint() const
- { return CPoint((short) h, (short) v); }
-
- // Creates a CPoint by casting the VCoordinate values to short values. In other
- // words, it throws away the top 16 bits of each long word value. If the values
- // don't fit within 16 bits you will get strange results (i.e. 32769 -> 1).
- // Danger: Don't use this in TView methods for converting from view space to QD
- // space. Instead, use TView::ViewToQDPt()!
-
- CPoint ToPoint() const;
- // Creates a CPoint by limiting the VCoordinate values to +/- 32K and casting
- // them to short values. This produces reasonable results if the values were
- // more than 16 bits (i.e. 32769 -> 32767).
- // Danger: Don't use this in TView methods for converting from view space to QD
- // space. Instead, use TView::ViewToQDPt()!
-
- //------------------------------------------------------------------------------------
- // Selector operators, two are needed one for operating on const VPoints and the other
- // on non-const VPoints.
- // Because of inlining, constant sel parameters resolve at compile time.
- //------------------------------------------------------------------------------------
-
- inline VCoordinate& operator[](VHSelect sel)
- { return (sel == vSel) ? v : h; }
- // For non-const VPoint
-
- inline const VCoordinate& operator[](VHSelect sel) const
- { return (sel == vSel) ? v : h; }
- // For const VPoint
-
-
- //------------------------------------------------------------------------------------
- // Arithmetic operators
- //------------------------------------------------------------------------------------
-
- VPoint operator+(const VPoint& pt) const;
- // c = a + b (a,b and c are VPoints)
-
- VPoint operator-(const VPoint& pt) const;
- // c = a - b (a,b and c are VPoints)
-
- VPoint operator-() const;
- // c = -a negate a
-
- VPoint& operator+=(const VPoint& pt);
- // c += a (a and c are VPoints, and
- // a reference to a is returned,
- // so these can be strung together)
-
- VPoint& operator-=(const VPoint& pt);
- // c -= a ( ditto )
-
-
- //------------------------------------------------------------------------------------
- // Relational operators, simply apply the relation to both coordinates if condition
- // holds for both coordinates then holds for the CPoint. Exception is != if either
- // coordinate is not equal then points are not equal.
- //------------------------------------------------------------------------------------
-
- Boolean operator!=(const VPoint& pt) const;
-
- Boolean operator==(const VPoint& pt) const;
-
- Boolean operator>(const VPoint& pt) const;
-
- Boolean operator<(const VPoint& pt) const;
-
- Boolean operator>=(const VPoint& pt) const;
-
- Boolean operator<=(const VPoint& pt) const;
-
-
- void ConstrainTo(const VRect& rt);
- // If the VPoint is not already inside the rectangle move the VPoint to the nearest
- // edge.
-
- protected:
-
- inline VCoordinate Min(const VCoordinate a,
- const VCoordinate b) const
- { return a < b ? a : b; }
-
- inline VCoordinate Max(const VCoordinate a,
- const VCoordinate b) const
- { return a > b ? a : b; }
-
- inline VCoordinate MinMax(VCoordinate MinVal,
- VCoordinate expression,
- VCoordinate MaxVal) const
- { return Min(Max(expression, MinVal), MaxVal); }
- // Returns the bounded minimum and maximum
- };
-
-
- //----------------------------------------------------------------------------------------
- // VRect is a toolbox compatible class for the old struct VRect. It is bitwise compatible
- // with the old struct because it contains no virtual functions.
- //----------------------------------------------------------------------------------------
-
- class VRect
- {
- public:
- VCoordinate top;
- VCoordinate left;
- VCoordinate bottom;
- VCoordinate right;
-
- //------------------------------------------------------------------------------------
- // VRect constructors
- //------------------------------------------------------------------------------------
-
- inline VRect()
- { }
-
- inline VRect(VCoordinate iLeft, VCoordinate iTop, VCoordinate iRight, VCoordinate iBottom) :
- top(iTop),
- left(iLeft),
- bottom(iBottom),
- right(iRight)
- { }
-
-
- inline VRect(const VPoint& topLeftPt, const VPoint& botRightPt)
- {
- *(VPoint*)&this->top = *(const VPoint*)&topLeftPt;
- *(VPoint*)&this->bottom = *(const VPoint*)&botRightPt;
- }
-
- inline VRect(const CRect& aRect) :
- top(aRect.top),
- left(aRect.left),
- bottom(aRect.bottom),
- right(aRect.right)
- { }
-
- inline VRect(const Rect& aRect) :
- top(aRect.top),
- left(aRect.left),
- bottom(aRect.bottom),
- right(aRect.right)
- { }
-
- #if qPowerPC
- // copy constructor
- inline VRect(const VRect& aVRect)
- {
- *(VPoint*)&this->top = *(const VPoint*)&aVRect.top;
- *(VPoint*)&this->bottom = *(const VPoint*)&aVRect.bottom;
- }
- #endif
-
- //------------------------------------------------------------------------------------
- // Copy and conversion operator methods
- //------------------------------------------------------------------------------------
-
- #if qPowerPC
- inline VRect& operator=(const VRect& aVRect)
- {
- *(VPoint*)&this->top = *(const VPoint*)&aVRect.top;
- *(VPoint*)&this->bottom = *(const VPoint*)&aVRect.bottom;
- return *this;
- }
- #endif
-
-
- #if qDebug
- // Conversion operator, for converting VRect to a textual representation of a
- // VRect. "VRect(top,righ,bottom,left)".
- inline operator const char*() const
- {
- char textRect[64];
- sprintf (textRect, "VRect(%d, %d, %d, %d)", left, top, right, bottom);
- return CChar63(textRect);
- }
- #endif
-
- inline CRect AsRect() const
- { return CRect((short) left, (short) top, (short) right, (short) bottom); }
- // Creates a CRect by casting the VCoordinate values to short values. In other
- // words, it throws away the top 16 bits of each long word value. If the values
- // don't fit within 16 bits you will get strange results (i.e. 32769 -> 1).
- // Danger: Don't use this in TView methods for converting from view space to QD
- // space. Instead, use TView::ViewToQDRect()!
-
- CRect ToRect() const;
- // Creates a CRect by limiting the VCoordinate values to +/- 32K and casting
- // them to short values. This produces reasonable results if the values were
- // more than 16 bits (i.e. 32769 -> 32767).
- // Danger: Don't use this in TView methods for converting from view space to QD
- // space. Instead, use TView::ViewToQDRect()!
-
-
-
- //------------------------------------------------------------------------------------
- // VPoint selectors for VRect. One for operating on const VRects and one for non-const
- // VRects.
- // Because of inlining, constant sel parameters resolve at compile time.
- //------------------------------------------------------------------------------------
-
- inline VPoint& operator[](PointSelector sel)
- { return (sel == topLeft) ? (*((VPoint *) &top)) : (*((VPoint *) &bottom)); }
- // Selector for non-const VRects
-
- inline const VPoint& operator[](PointSelector sel) const
- { return (sel == topLeft) ? (*((const VPoint *) &top)) : (*((const VPoint *) &bottom)); }
- // Selector for const VRects
-
-
- //------------------------------------------------------------------------------------
- // Operators for adding and subtracting one VRect from to/from another.
- //------------------------------------------------------------------------------------
-
- VRect operator+(const VRect& rt) const;
-
- VRect operator-(const VRect& rt) const;
-
- VRect& operator+=(const VRect& rt);
-
- VRect& operator-=(const VRect& rt);
-
-
- //------------------------------------------------------------------------------------
- // Operators overloaded for adding and subtracting some increment to/from each CPoint
- // along both axis. Very convenient for translating VRects. These take a CPoint and
- // since VPoint has a constructor that takes two VCoordinates the VRect windowVRect
- // can be translated 100 pixels in the positive y direction by the statement:
- //
- // windowVRect = windowVRect + VPoint (0, 100); or
- // windowVRect += VPoint (0, 100);
- //------------------------------------------------------------------------------------
-
- VRect operator+(const VPoint& pt) const;
-
- VRect operator-(const VPoint& pt) const;
-
- VRect operator-() const;
- // Negation
-
- VRect& operator+=(const VPoint& pt);
-
- VRect& operator-=(const VPoint& pt);
-
-
- //------------------------------------------------------------------------------------
- // Miscellaneous methods
- //------------------------------------------------------------------------------------
-
- Boolean operator==(const VRect& rt) const;
- // Returns true if all four coordinates are equal
-
- Boolean operator!=(const VRect& rt) const;
- // Returns true if at least one of the four coordinates are not equal.
-
- VRect operator &(const VRect& rt) const;
- // Returns the intersection of 'this' and 'rt'.
-
- VRect operator |(const VRect& rt) const;
- // Returns the union of 'this' and 'rt'. Actually not the true mathematical union
- // but the smallest rectangle that bounds both 'this' and 'rt'.
-
- VRect& Inset(const VPoint& delta);
- // Inset a VRect by VPoint.
-
- Boolean Valid() const;
- // Returns true if a valid rectangle (left < right and top < bottom).
-
- void Validate();
- // Validate rectagle by switching the points if right-bottom is above or to the
- // left of left-top.
-
- Boolean Empty() const;
- // Return true if the VRect is empty (right - left = 0 and bottom - top = 0)
-
-
- VCoordinate GetLength(VHSelect sel) const;
- // Returns the length of a VRect in a given dimension.
-
- VPoint GetSize() const;
- // Returns the size of a VRect in a VPoint.
-
- Boolean Contains(const VPoint& pt) const;
- // Returns true if 'pt' is contained in the rectangle 'this'
-
- Boolean Contains(const VRect& rt) const;
- // Return true if 'rt' is contained in the rectangle 'this'
-
- protected:
-
- inline VCoordinate Min(const VCoordinate a,
- const VCoordinate b) const
- { return a < b ? a : b; }
-
- inline VCoordinate Max(const VCoordinate a,
- const VCoordinate b) const
- { return a > b ? a : b; }
-
- inline VCoordinate MinMax(VCoordinate MinVal,
- VCoordinate expression,
- VCoordinate MaxVal) const
- { return Min(Max(expression, MinVal), MaxVal); }
- // Returns the bounded minimum and maximum
- };
-
-
- //========================================================================================
- // The following global routines were originally for Pascal compatibility. They are basically
- // wrappers that call back into the methods in VRect and VPoint. C++ programs should use
- // the methods in VRect and VPoint to avoid the additional function call overhead.
- //
- // Left in for compatibility.
- //========================================================================================
-
- void PtToVPt(const CPoint thePt, VPoint& theVPt);
- CPoint VPtToPt(const VPoint& theVPt);
- void RectToVRect(const CRect& theRect, VRect& theVRect);
- void VRectToRect(const VRect& theVRect, CRect& theRect);
- void AddVPt(const VPoint& srcVPt, VPoint& dstVPt);
- void SubVPt(const VPoint& srcVPt, VPoint& dstVPt);
- void SetVPt(VPoint& vPt, VCoordinate h, VCoordinate v);
- Boolean EqualVPt(const VPoint& aVPt, const VPoint& bVPt);
- void SetVRect(VRect& vRt,
- VCoordinate left, VCoordinate top,
- VCoordinate right, VCoordinate bottom);
- void OffsetVRect(VRect& vRt, VCoordinate dh, VCoordinate dv);
- void InsetVRect(VRect& vRt, VCoordinate dh, VCoordinate dv);;
- void Pt2VRect(const VPoint& theTopLeft, const VPoint& theBotRight, VRect& vRt);
- Boolean PtInVRect(const VPoint& vPt, const VRect& vRt);
- Boolean EmptyVRect(const VRect& vRt);
- Boolean EqualVRect(const VRect& aVRt, const VRect& bVRt);
- VCoordinate LengthVRect(const VRect& vRt, VHSelect whichDim);
- void PinVRect(const VRect& vRt, VPoint& vPt);
- Boolean SectVRect(const VRect& src1, const VRect& src2, VRect& dst);
- void UnionVRect(const VRect& src1, const VRect& src2, VRect& dst);
-
- #endif
-